package com.bookcode.controller;


import com.bookcode.entity.Sign;
import com.bookcode.entity.SignRecorder;
import com.bookcode.service.*;
import com.bookcode.entity.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SignController {

    @Autowired
    SignService signService;
    @Autowired
    SignRecorderService signRecorderService;
    @Autowired
    CourseService courseService;
    @Autowired
    CourseStudentService courseStudentService;
    @Autowired
    StudentService studentService;


    /*
     * 发起签到
     * */
   @PostMapping(value = "/course/{courseId}/sign")
   public HttpResult add(@PathVariable Long courseId) {
        HttpResult result = new HttpResult();
        try {
            if (signService.findSignsNotEndByCourse(courseId).size() != 0) {
                throw new Exception("本课程有签到尚未结束，请先结束签到！");
            }
            Sign sign = new Sign();
            sign.setEnd(false);
            sign.setCourseId(courseId);
            signService.save(sign);
            result.setStatus("成功发起签到。");
            result.setMessage("课程" + courseId.toString() + "发起了签到");
        } catch (Exception e) {
            if (!e.getMessage().isEmpty()) {
                result.setMessage(e.getMessage());
            }
            result.setStatus("该课程处在可以签到的状态，不能重复发起签到。");
        } finally {
            return result;
        }

    }

    /*
     * 结束签到
     * */
  @PutMapping(value = "/sign/{id}/end")
   public HttpResult end(@PathVariable Long id) {

        HttpResult result = new HttpResult();
        try {
            if (signService.signIsEnd(id)) {
                throw new Exception("签到已经结束");
            }
            Sign sign = signService.findSignById(id);
            sign.setEnd(true);
            signService.save(sign);
            result.setStatus("本课程签到结束了。");
            result.setMessage("课程" + id.toString() + "结束了签到");
        } catch (Exception e) {
            if (!e.getMessage().isEmpty()) {
                result.setMessage(e.getMessage());
            }
            result.setStatus("本课程已经结束签到，无须重复结束。");
        } finally {
            return result;
        }

    }


    /*
     * 查询课程所有发起的签到
     * */
    @GetMapping(value = "/course/{courseId}/signs")
    public HttpResult querySignsByCourse(@PathVariable Long courseId) {
        HttpResult<List<Sign>> result = new HttpResult<>();
        List<Sign> signs = signService.findSignsByCourse(courseId);
        result.setData(signs);
        result.setStatus("课程所有发起的签到：");
        return result;
    }

    //签到记录
    @PostMapping("/addSignRecorders")
    public HttpResult courseStudent(@ModelAttribute SignRecorder signRecorder) {
        Long lcourseId = signRecorder.getCourseId();
        Long lstudentId = signRecorder.getStudentId();
        HttpResult result = new HttpResult();
        try {
            if (courseStudentService.validCourse(lcourseId)) {
                if (signService.signIsEnd(lcourseId)) {
                    if (courseStudentService.isStudentOfCourse(lcourseId, lstudentId)) {
                        if (signRecorderService.isSigned(lcourseId, lstudentId)) {
                            result.setStatus(studentService.findStudentById(lstudentId).getName() + "已经签到。");
                        } else {
                            signRecorderService.save(signRecorder);
                            result.setStatus(studentService.findStudentById(lstudentId).getName() + "签到课程《" + courseService.findCourseById(lcourseId).getName() + "》成功。");
                        }
                    }
                } else {
                    result.setStatus("该课程没有发起签到或者签到已经结束。");                }
            } else {
                result.setStatus("该课程无效。");
            }
        } catch (Exception e) {
            result.setStatus("非法签到。");
        } finally {
            return result;
        }
    }

    @PostMapping(value = "/beginsign",params = {"courseId"})
    public HttpResult beginsign(Long courseId)  {

        HttpResult result = new HttpResult();
        try {
            if (signService.findSignsNotEndByCourse(courseId).size() != 0) {
                throw new Exception("本课程有签到尚未结束，请先结束签到！");
            }
            Sign sign = new Sign();
            sign.setEnd(false);
            sign.setCourseId(courseId);
            signService.save(sign);
            result.setStatus("成功发起签到。");
            result.setMessage("课程" + courseId.toString() + "发起了签到");
        } catch (Exception e) {
            if (!e.getMessage().isEmpty()) {
                result.setMessage(e.getMessage());
            }
            result.setStatus("该课程处在可以签到的状态，不能重复发起签到。");
        } finally {
            return result;
        }


    }

    @PostMapping(value = "/endsign",params = {"courseId"})
    public HttpResult endsign(Long courseId) {
        HttpResult result = new HttpResult();
        try {
            if (signService.signIsEnd(courseId)) {
                result.setStatus("签到已经结束");
            }
           Sign  sign = signService.findSignById(courseId);
            sign.setEnd(true);

            signService.save(sign);
            result.setStatus("本课程成功结束了签到。");
            result.setMessage("课程" + courseId.toString() + "结束了签到");
        } catch (Exception e) {
            if (!e.getMessage().isEmpty()) {
                result.setMessage(e.getMessage());
            }
            result.setStatus("本课程已经结束签到，无须重复结束；或者尚未发起签到。");
        } finally {
            return result;
        }


    }




}
